<?php
namespace app\api\controller;

use app\model\AdminModel;
use app\model\MotifModel;

use app\model\TopicModel;
use think\facade\Request;
use think\facade\Validate;
use think\Model;

class homepage  {
    public function homepage_list(){
        session_start();
        $name = $_SESSION['admin_name'];
        $motif = MotifModel::where('motif_state','=',1)->select();
        $motif_arr = [[]];
        foreach($motif as $key => $value){
            $motif_arr[$key][]= $value['motif_id'];
            $motif_arr[$key][] = $value['motif_name'];
            $motif_arr[$key][] = $value['motif_them'];
            $motif_arr[$key][] = date('Y-m-d H:i:s',$value['add_time']);
            $motif_arr[$key][] = date('Y-m-d H:i:s',$value['update_time']);
        }
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [$name,$motif_arr]
        ];
        return json($data);
    }
    //删除
    public function homepage_delete(){
        $motifid = Request::param('motifid');
        $motif = MotifModel::where('motif_id','=',$motifid)->find();
        $motif['motif_state']=2;
        $motif['update_time'] = time();
        $result = $motif->save();
        if($result){
            $data = [
                'status' => 0,
                'message' => '删除成功',
                'data' => []
            ];
            return json($data);
        }else{
            $data = [
                'status' => 10001,
                'message' => '删除失败',
                'data' => []
            ];
            return json($data);
        }

    }
    //motif表数据添加
    public function homepage_motif(){
        //用户id
        session_start();
        $adminid = $_SESSION['admin_id'];
        $motif = Request::param();

        //判断题目是否有空的
        foreach($motif['topic'] as $time){
            if($time == ''){
                $data = [
                    'status' => 10001,
                    'message' => '题目不能为空',
                    'data' => []
                ];
                return json($data);
            }
        }
        //判断最小分数是否有空的
        foreach($motif['lift'] as $time){
            if($time == ''){
                $data = [
                    'status' => 10002,
                    'message' => '最小分数不能为空',
                    'data' => $motif
                ];
                return json($data);
            }
        }
        //判断最大分数是否有空的
        foreach($motif['right'] as $time){
            if($time == ''){
                $data = [
                    'status' => 10003,
                    'message' => '最大分数不能为空',
                    'data' => []
                ];
                return json($data);
            }
        }

        $validate = Validate::rule([
            'theme|主题' => 'require|min:2|max:50',
            'explain|主题说明' => 'require|min:2|max:255',
            'name|问卷名称' => 'require|min:2|max:50',
        ]);
        $check = $validate->check($motif);
        if(!$check){
            $data = [
                'status' => 10004,
                'message' => $validate->getError(),
                'data' => $motif
            ];
            return json($data);
        }
        //主题是否有相同的
        $motifid = MotifModel::where('motif_them','=',$motif['theme'])->find();
        if($motifid){
            $data = [
                'status' => 10004,
                'message' => '这个主题已经存在',
                'data' => []
            ];
            return json($data);
        }
        //把主题添加到motif表
        $result = MotifModel::create([
            'motif_name' => $motif['name'],
            'motif_them' => $motif['theme'],
            'motif_explain' => $motif['explain'],
            'admin_id' => $adminid,
            'add_time' => time()
        ]);

        if($result){
            //主题id
            $id = MotifModel::where('motif_them','=',$motif['theme'])->find();
            for($i = 0;$i<count($motif['topic']);$i++) {
                $topic = TopicModel::create([
                    'topic_name' =>$motif['topic'][$i],
                    'score_lift' => $motif['lift'][$i],
                    'score_right' => $motif['right'][$i],
                    'motif_id' => $id['motif_id']
                ]);
            }
            if($topic){
                $_SESSION['motif_id']=$id['motif_id'];
                $data = [
                    'status' => 0,
                    'message' => '保存成功',
                    'data' => []
                ];
                return json($data);
            }else{
                $data = [
                    'status' => 10006,
                    'message' => '保存失败',
                    'data' => []
                ];
                return json($data);
            }
        }
        else{
            $data = [
                'status' => 10005,
                'message' => '数据添加失败',
                'data' => []
            ];
            return json($data);
        }

        //把题目信息添加到topic

    }
    public function a(){

    }
}